package com.ymdx.util.tool;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.util.Assert;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * common util
 * @author zh
 *
 */
public class CommonUtil {
	/**
	 * judge whether the given str is null
	 * @param str 
	 * @return the default value is false
	 */
	public static boolean isEmpty(String str){
		if(str == null || str.trim().equals("")){
			return true;
		}
		
		return false;
	}
	
	/**
	 * judge whether the given value is null
	 * @param value 
	 * @return the default value is false
	 */
	public static boolean isEmpty(Long value){
		if(value == null || value == 0){
			return true;
		}
		
		return false;
	}
	
	
	/**
	 * judge whether the given value is null
	 * @param value 
	 * @return the default value is false
	 */
	public static boolean isEmpty(Integer value){
		if(value == null || value == 0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * judge whether the given list is null
	 * @param str 
	 * @return the default value is false
	 */
	public static boolean isEmpty(List<?> list){
		if(list == null || list.size() == 0){
			return true;
		}
		
		return false;
	}
	
	/**
	 * judge whether the given map is null
	 * @param str 
	 * @return the default value is false
	 */
	public static boolean isEmpty(Map map){
		if(map == null){
			return true;
		}
		
		return map.isEmpty();
	}
	
	/**
	 * parse the given obj to String
	 * @param obj
	 * @return
	 */
	public static String toStr(Object obj){
		if(obj == null){
			return null;
		}
		
		if(obj instanceof String){
			return obj.toString();
		}else if(obj instanceof Integer){
			return Integer.toString((Integer)obj);
		}else if(obj instanceof Float){
			return Float.toString((Float)obj);
		}else if(obj instanceof Short){
			return Short.toString((Short)obj);
		}else if(obj instanceof Long){
			return Long.toString((Long)obj);
		}else if(obj instanceof Double){
			return Double.toString((Double)obj);
		}else if(obj instanceof Boolean){
			return Boolean.toString((Boolean)obj);
		}else if(obj instanceof Character){
			return Character.toString((Character)obj);
		}else{
			return null;
		}
	}
	
	/**
	 * transform the given methodName into the method of get
	 * @param methodName
	 * @return
	 */
	public static String getGetMethod(String methodName){
		int lower = methodName.charAt(0);
		int upper = lower - 32;
		char[] charArray = methodName.toCharArray();
		charArray[0] = (char)upper;
		methodName = "get" + new String(charArray);
		
		return methodName;
	}
	
	/**
	 * transform the given str into the method of set
	 * @param name
	 * @return
	 */
	public static String getSetMethod(String name){
		int lower = name.charAt(0);
		int upper = lower - 32;
		char[] charArray = name.toCharArray();
		charArray[0] = (char)upper;
		name = "set" + new String(charArray);
		
		return name;
	}
	
	public static Method getMethodByName(Method[] methods,String methodName){
		Assert.notNull(methodName, "No methodName specified");
		
		Method result = null;
		
		for (Method method : methods) {
			if (methodName.equals(method.getName())) {
				result = method;
				
				break;
			}
		}
				
		return result;
	}
	
//	public static String encodePassword(String rawPass){
//		ShaPasswordEncoder encoder = new ShaPasswordEncoder(256);
//		return encoder.encodePassword(rawPass, null);
//	}
	
	public static String randomOrderNum(){
		SimpleDateFormat df=new SimpleDateFormat("yyyyMMddHHmmss");
		String str=df.format(new Date())+ RandomStringUtils.randomNumeric(5);
		return str;
	}
	
	public static String generateNonceStr(){
		
		return UUID.randomUUID().toString().replace("-", "").substring(0, 16);
	}
	
	public static Date parseDate(String source,String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			return sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public static String getResult(HttpServletRequest request){
		StringBuilder sb = new StringBuilder();
		
		try {
			ServletInputStream in = request.getInputStream();
			int len;
			byte[] b = new byte[1024];
			while( (len = in.read(b,0,1024)) != -1){
				sb.append(new String(b, 0, len));
				b = null;
				b = new byte[1024];
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return sb.toString().trim();
	}
	
	/**
	 * 使字符串的第一个字母大写
	 * 
	 * @param src
	 * @author ljx
	 * @return
	 */
	public static String firstToUppercase(String src) {
		StringBuilder sb = new StringBuilder(src);
		sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 使字符串的第一个字母小写
	 * 
	 * @param src
	 * @author ljx
	 * @since 2017-03-30
	 * @return
	 */
	public static String firstToLowercase(String src) {
		StringBuilder sb = new StringBuilder(src);
		sb.setCharAt(0, Character.toLowerCase(sb.charAt(0)));
		return sb.toString();
	}

	/**
	 * 如果原字符串有这个前缀，则去除
	 * 
	 * @param src
	 *            原字符串
	 * @param prefix
	 *            前缀
	 * @return
	 */
	public static String removePrefix(String src, String prefix) {
		if (src.startsWith(prefix)) {
			src = src.substring(prefix.length());
		}
		return src;
	}
}
